import Vue from "vue";
import App from "./App.vue";
import Login from "./views/Login";
import router from "./router";
import store from "./store";
import axios from "axios";

import "@/filters";
import "@/components";

//引入element-ui核心库
import ElementUI from 'element-ui';
//引入element-ui相关样式
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

//请求拦截
axios.interceptors.request.use(config => {
  store.commit("CHANGE_LOADING", true);
  //防止缓存
  // console.log(config);
  config.url = '/gsc' + config.url + (config.url.includes('?') ? '&' : '?') + 't=' + Date.now();
  let headerToken = '';
  if (localStorage.admintoken) { headerToken = localStorage.admintoken }
  if (localStorage.usertoken) { headerToken = localStorage.usertoken }
  config.headers = {
    headerToken
  }
  return config;
});
//响应拦截
axios.interceptors.response.use(({ data }) => {
  store.commit("CHANGE_LOADING", false);
  if (data.ok === -2) {
    store.commit('OUT_LOGIN');
  } else {
    if (data.pageSum) {
      store.commit('CHANGE_PAGE_SUM', data.pageSum);
    }
  }
  return data;
})

Vue.config.productionTip = false;
Vue.prototype.$axios = axios;

new Vue({
  router,
  store,
  data: {
    isLogin: true,
    isAdmin: false
  },
  render: function (h) {
    // console.log(store.state.islogin);
    return h(store.state.islogin ? App : Login)
  },

}).$mount("#app");
